package com.example.cafe.work.service.impl;

import com.example.cafe.work.dto.Result;
import com.example.cafe.work.mapper.OrderMapper;
import com.example.cafe.work.mapper.TicketMapper;
import com.example.cafe.work.model.SeckillTicket;
import com.example.cafe.work.mapper.SeckillTicketMapper;
import com.example.cafe.work.model.Ticket;
import com.example.cafe.work.model.TicketOrder;
import com.example.cafe.work.service.SeckillTicketService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.cafe.work.service.TicketOrderService;
import com.example.cafe.work.service.TicketService;
import com.example.cafe.work.util.RedisIdWorker;
import com.example.cafe.work.util.UserHolder;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.time.LocalDateTime;

import static com.example.cafe.work.util.RedisConstants.SECKILL_STOCK_KEY;

/**
 * <p>
 * 秒杀优惠券表，与优惠券是一对一关系 服务实现类
 * </p>
 *
 * @author hua
 * @since 2024-09-23
 */
@Service
public class SeckillTicketServiceImpl extends ServiceImpl<SeckillTicketMapper, SeckillTicket> implements SeckillTicketService {
    @Resource
    TicketMapper ticketMapper;
    @Resource
    RedisIdWorker redisIdWorker;
    @Autowired
    TicketOrderService ticketOrderService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;



    @Transactional
    @Override
    public void addSeckillVoucher(Ticket ticket) {
        ticketMapper.add(ticket);
        //想办法把id绑了，请求没传id
        ticket.setTicketId(2);
        SeckillTicket seckillTicket = new SeckillTicket();
        seckillTicket.setTicketId(ticket.getTicketId());
        seckillTicket.setBeginTime(ticket.getBeginTime());
        seckillTicket.setEndTime(ticket.getEndTime());
        seckillTicket.setStock(ticket.getStock());
        save(seckillTicket);
        stringRedisTemplate.opsForValue().set(SECKILL_STOCK_KEY+ticket.getTicketId(),ticket.getStock().toString());
    }

    @Override
    public Result seckill(Integer ticketId) {
        //拿到目标优惠劵
        SeckillTicket seckillTicket = getById(ticketId);
        //判断是否开始
        LocalDateTime beginTime = seckillTicket.getBeginTime();
        if(beginTime.isAfter(LocalDateTime.now())){
            return Result.ok("秒杀还未开始");
        }
        //判断是否结束
        LocalDateTime endTime = seckillTicket.getEndTime();
        if(endTime.isBefore(LocalDateTime.now())){
            return Result.ok("秒杀已经结束");
        }
        //判断库存是否充足
        if (seckillTicket.getStock()<1){
            return Result.ok("库存不足");
        }
        boolean success = update().setSql("stock=stock-1")
                .eq("ticket_id", seckillTicket.getTicketId()).gt("stock",0)
                .update();
        //创建订单（优惠劵的）
        TicketOrder ticketOrder = new TicketOrder();
        ticketOrder.setTicketId(seckillTicket.getTicketId());
        ticketOrder.setUserId(UserHolder.getUser().getUserId());
        ticketOrder.setId(redisIdWorker.nextId("order"));
        ticketOrderService.save(ticketOrder);
        return Result.ok(ticketOrder.getId());
    }
}
